<script setup lang="ts">
import type { Todo } from '@/types';
import TodoItem from './TodoItem.vue';
import draggable from 'vuedraggable'; 

const props = defineProps<{
  title: string;          
  todos: Todo[];            
  editingId: number | null; 
  editText: string;         
}>();

const emit = defineEmits<{
  (e: 'toggle-status', id: number): void;
  (e: 'delete', id: number): void;
  (e: 'start-edit', todo: Todo): void;
  (e: 'finish-edit', id: number, content: string): void;
  (e: 'cancel-edit'): void;
  (e: 'update-edit-text', value: string): void;
  (e: 'drag-end'): void; 
}>();

const handleDragEnd = () => {
  emit('drag-end');
};
</script>

<template>
  <div class="todo-column">
    <!-- 列标题 -->
    <h3>{{ title }}</h3>
    
    <!-- 待办列表 -->
    <draggable 
      :list="todos" 
      item-key="id"
      class="todo-list"
      ghost-class="ghost"
      @end="handleDragEnd"
    >
      <template #item="{ element }">
        <li class="todo-item-container">
          <TodoItem
            :todo="element"
            :editing-id="editingId"
            :edit-text="editText"
            @toggle-status="(id) => emit('toggle-status', id)"
            @delete="(id) => emit('delete', id)"
            @start-edit="(todo) => emit('start-edit', todo)"
            @finish-edit="(id, content) => emit('finish-edit', id, content)"
            @cancel-edit="() => emit('cancel-edit')"
            @update-edit-text="(value) => emit('update-edit-text', value)"
          />
        </li>
      </template>
    </draggable>
  </div>
</template>

<style scoped>
.todo-column {
  flex: 1;
  overflow: hidden;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.todo-column h3 {
  text-align: center;
  color: #333;
  border-bottom: 2px solid #eee;
  padding-bottom: 8px;
  margin: 0 0 15px 0;
}

.todo-column:nth-child(1) {
  background: linear-gradient(135deg, #e0f7fa 0%, #f5f5f5 100%);
}

.todo-column:nth-child(2) {
  background: linear-gradient(135deg, #e8f5e9 0%, #f5f5f5 100%);
}

/* 待办列表 */
.todo-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* 单个待办项 */
.todo-item-container {
  padding: 10px;
  min-height: 60px;
  margin: 0 0 8px 0;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 4px;
  font-size: 16px;
  transition: background-color 0.2s ease;
  gap: 30px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.todo-item-container:hover {
  background-color: rgba(255, 255, 255, 1);
}

/* 拖拽元素样式 */
.ghost {
  background-color: #e0e0e0 !important;
  opacity: 0.7;
}

/* 未完成列按钮样式 */
.todo-column:nth-child(1) .status-btn {
  background-color: #42b983;
  color: white;
}

.todo-column:nth-child(1) .edit-btn {
  background-color: #2196f3;
  color: white;
}

.todo-column:nth-child(1) .status-btn:hover {
  background-color: #435469;
}

.todo-column:nth-child(1) .edit-btn:hover {
  background-color: #0b7dda;
}

/* 已完成列按钮样式 */
.todo-column:nth-child(2) .status-btn,
.todo-column:nth-child(2) .edit-btn {
  background-color: #999;
  color: white;
}

.todo-column:nth-child(2) .status-btn:hover,
.todo-column:nth-child(2) .edit-btn:hover {
  background-color: #777;
}
</style>